package com.gopay.solution.domain.bo.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.stereotype.Service;

import com.gopay.common.constants.txncd.IntTxnCd;
import com.gopay.common.domain.solution.TransReq;
import com.gopay.common.domain.solution.TransRes;
import com.gopay.solution.domain.po.SolutionRuleDtl;
import com.gopay.solution.domain.po.SsTransDtl;
import com.gopay.solution.domain.vo.Event;
import com.gopay.solution.domain.vo.Result;

/**
 * 方案服务 交易方案 规则执行器
 * 
 * @SsTransSoluRuleExecutor.java
 * @author MA HUAN
 * @2013-3-27 下午4:29:26 www.gopay.com.cn Inc.All rights reserved.
 */
@Service(value = "ssTransSoluRuleExecutor")
public class SsTransSoluRuleExecutor extends AbstractSolutionRuleExecutor {

    @Override
    public Result execute(List<SolutionRuleDtl> rules, Event event) {
        

    	logger.error("SsTransSoluRuleExecutor execute begin: "+rules.size());
        Result result = new Result();
        result.setSolved(false);
        result.setFailedReason("");
        result.setDetail(null);

        if (null == rules || rules.isEmpty()) {
            result.setFailedReason("查询不到交易方案记录");
            return result;
        }

        Serializable input = event.getDetail();

        TransReq req = (TransReq) input;
        IntTxnCd intTxnCd = req.getIntTxnCd();

        boolean isAvailable = false;
        //生产bug8期 lx 2017-8-10 13:39:31 由于生产打印日志量太大，占用磁盘空间，所以注释该段代码打印，开发测试调试时可以放开，生产需关闭
        //logger.error("SsTransSoluRuleExecutor execute intTxnCd: "+intTxnCd);
        //logger.error("SsTransSoluRuleExecutor execute req: "+req);
        //logger.error("SsTransSoluRuleExecutor execute rules: "+rules);
        
        SsTransDtl dtl = null;
        for (SolutionRuleDtl rule : rules) {
            
            dtl = (SsTransDtl)rule;
            if(dtl.getGopayIntTxnCd().equals(intTxnCd.value)) {
                isAvailable = true;
                break;
            }
        }
        //生产bug8期 lx 2017-8-10 13:39:31 由于生产打印日志量太大，占用磁盘空间，所以注释该段代码打印，开发测试调试时可以放开，生产需关闭
        //logger.error("SsTransSoluRuleExecutor execute isAvailable: "+isAvailable);
        TransRes res = new TransRes();
        res.setAvailable(isAvailable);
        result.setDetail(res);
        
        if(isAvailable){
            result.setSolved(true);
        }else{
            result.setFailedReason("查询不到交易方案记录");
        }
        
        logger.error("SsTransSoluRuleExecutor execute result: "+result);
        return result;
    }
}
